package com.xxhui.fragmentdemo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.xxhui.fragmentdemo.Framment.FragmentA;
import com.xxhui.fragmentdemo.Framment.FragmentB;
import com.xxhui.fragmentdemo.Framment.FragmentBackStack;

public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener {
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    FragmentA fragmentA;
    FragmentB customFragment;
    FragmentBackStack fragmentBackStack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentA = (FragmentA) fragmentManager.findFragmentById(R.id.FragmentA);
        addFragmentByJava();
        useBackStack();
    }

    private void addFragmentByJava(){
        customFragment = new FragmentB();
        fragmentTransaction.add(R.id.Fragment_container,customFragment);
        //fragmentTransaction.commit();
    }

    private void useBackStack(){
        fragmentBackStack = new FragmentBackStack();
        fragmentTransaction.replace(R.id.Fragment_container,fragmentBackStack);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    @Override
    public void sendMsg(String msg) {
        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
    }

    public void send(View v){
        Toast.makeText(MainActivity.this,"i am activity",Toast.LENGTH_SHORT).show();
    }

}
